// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include <windows.h>
#include "WerewulfCamera.h"

MgcImplementRTTI(WerewulfCamera,MgcCamera);
MgcImplementStream(WerewulfCamera);

//----------------------------------------------------------------------------
WerewulfCamera::WerewulfCamera (MgcReal fWidth, MgcReal fHeight)
{
    m_fWidth = fWidth;
    m_fHeight = fHeight;

    OnFrustumChange();
    OnViewPortChange();
    OnFrameChange();
}
//----------------------------------------------------------------------------
WerewulfCamera::WerewulfCamera ()
{
    m_fWidth = 0.0;
    m_fHeight = 0.0;
}
//----------------------------------------------------------------------------
void WerewulfCamera::OnFrustumChange ()
{
    MgcCamera::OnFrustumChange();

    // set projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(m_fFrustumL,m_fFrustumR,m_fFrustumB,m_fFrustumT,m_fFrustumN,
        m_fFrustumF);
}
//----------------------------------------------------------------------------
void WerewulfCamera::OnViewPortChange ()
{
    MgcCamera::OnViewPortChange();

    // set view port
    GLint iX = (GLint)(m_fPortL*m_fWidth);
    GLint iY = (GLint)(m_fPortB*m_fHeight);
    GLsizei iW = (GLsizei)((m_fPortR - m_fPortL)*m_fWidth);
    GLsizei iH = (GLsizei)((m_fPortT - m_fPortB)*m_fHeight);
    glViewport(iX,iY,iW,iH);
}
//----------------------------------------------------------------------------
void WerewulfCamera::OnFrameChange ()
{
    MgcCamera::OnFrameChange();

    // set view matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    MgcVector3 kLookAt = m_kLocation + m_kDirection;
    gluLookAt(m_kLocation.x,m_kLocation.y,m_kLocation.z,kLookAt.x,kLookAt.y,
        kLookAt.z,m_kUp.x,m_kUp.y,m_kUp.z);
}
//----------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* WerewulfCamera::Factory (MgcStream& rkStream)
{
    WerewulfCamera* pkObject = new WerewulfCamera;
    MgcStream::Link* pkLink = new MgcStream::Link(pkObject);
    pkObject->Load(rkStream,pkLink);
    return pkObject;
}
//---------------------------------------------------------------------------
void WerewulfCamera::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcCamera::Load(rkStream,pkLink);

    // native data
    MgcStreamRead(rkStream,m_fWidth);
    MgcStreamRead(rkStream,m_fHeight);
}
//---------------------------------------------------------------------------
void WerewulfCamera::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcCamera::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool WerewulfCamera::Register (MgcStream& rkStream)
{
    return MgcCamera::Register(rkStream);
}
//---------------------------------------------------------------------------
void WerewulfCamera::Save (MgcStream& rkStream)
{
    MgcCamera::Save(rkStream);

    // native data
    MgcStreamWrite(rkStream,m_fWidth);
    MgcStreamWrite(rkStream,m_fHeight);
}
//---------------------------------------------------------------------------
